package com.hnguigu.ajax.controller;

import com.hnguigu.ajax.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/ajax")
public class AjaxController {

    @RequestMapping("/test1")
    public String test1() {
        return "test1";
    }

    @RequestMapping("/test3")
    public String test3() {
        return "test3";
    }

    @RequestMapping(value = "/test4")
    public String test4() {
        return "test4";
    }

    @RequestMapping("/exists")
    public void exists(String name, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            if ("zhangsan".equals(name)) {
                writer.print("<font color='red'>此用户已占用！</font>");
            } else {
                writer.println("此用户可以使用！");
            }

            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/test2")
    public String test2() {
        return "test2";
    }



    /*@RequestMapping(value = "/check", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> check(String name) {
        Map<String, Object> map = new HashMap();
        if ("zhangsan".equals(name)) {
            map.put("flag", "false");
            map.put("message", "<font color='red'>此用户已占用！</font>");
        } else {
            map.put("flag", "true");
            map.put("message", "此用户可以使用！");
        }
        return map;
    }*/


    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ResponseBody
    public Result check(String name) {
        if ("zhangsan".equals(name)) {
            return new Result("false", "5001", "<font color='red'>此用户已占用！</font>", null);
        } else {
            return new Result("true", "2000", "此用户可以使用：" + name, null);
        }
    }

    /**
     *
     * @param request 被装饰成了DefaultMultipartHttpServletRequest
     * @return
     */
    /*@RequestMapping(value = "/formData", method = RequestMethod.POST)
    @ResponseBody
    public Result formData(HttpServletRequest request) {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String salary = request.getParameter("salary");
        String birthday = request.getParameter("birthday");
        System.out.println("参数值为:" + name);
        if ("zhangsan".equals(name)) {
            return new Result("false", "5001", "<font color='red'>此用户已占用！</font>", null);
        } else {
            return new Result("true", "2000", "此用户可以使用：" + name, null);
        }
    }*/

    @RequestMapping(value = "/formData", method = RequestMethod.POST)
    @ResponseBody
    public Result formData(String name,Integer age) {
        System.out.println("参数值为:" + name);
        System.out.println("参数值为:" + age);
        if ("zhangsan".equals(name)) {
            return new Result("false", "5001", "<font color='red'>此用户已占用！</font>", null);
        } else {
            return new Result("true", "2000", "此用户可以使用：" + name, null);
        }
    }

}
